package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    private R<Page> page(int page, int pageSize, String name){
        //逻辑处理全写再业务层了
        return setmealService.bypage(page,pageSize,name);
    }

    /**
     * 添加套餐数据 同时添加套餐与菜品的关系数据
     * @param dto
     * @return
     */
    @PostMapping
    public R<String> savewithsetmealdish(@RequestBody SetmealDto dto){
        log.info("套餐信息{}",dto);
        //逻辑代码全部写在业务逻辑层了
        R<String> r = setmealService.saveWithSetmeadish(dto);
        return r;
    }

    /**
     * 修改套餐信息时响应数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Setmeal> showDate(@PathVariable Long id){
       //根据套餐id获取套餐信息
        Setmeal setmeal = setmealService.getById(id);
        return R.success(setmeal);
    }

    /**
     * 根据页面提交的数据修改套餐信息，同时修改套餐菜品关联表中的信息
     * @param dto
     * @return
     */
    public R<String> update(@RequestBody SetmealDto dto){
        setmealService.update(dto);
        return null;
    }

    /**
     * 删除套餐信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long ... ids){
        log.info("ids={}",ids);
        //这里使用可变参数,可同时实现删除单条数据和批量删除两个功能
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
       //删除套餐与套餐关联菜品表中的相关记录
        return  setmealService.deletewithdish(list);
    }

    /**
     * 停售      批量停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> stopBuy(Long ... ids){
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        List<Setmeal> setmeals = setmealService.listByIds(list);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        }
        return R.success("停售成功");
    }
    /**
     * 停售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> beginBuy(Long ... ids){
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        List<Setmeal> setmeals = setmealService.listByIds(list);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        }
        return R.success("起售成功");
    }
    /**
     * 前台中菜品的分类能查出来  但是套餐分类下的菜品查不出来  下面是点击查询套餐是的请求路径
     */
    //list?categoryId=1413342269393674242&status=1
     //直接返回一个setmeal对象就好了
    @GetMapping("/list")
    public R<List<Setmeal>> lookforsetmeal(Long categoryId,int status){
        log.info("category={}status={}",categoryId,status);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 前台查看套餐详细信息展示
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> searchDishs(@PathVariable String id){
        log.info("id={}",id);
        //1:创建构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //2:设置条件
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        //3:查询所有菜品信息
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //实现dish到dishdto的拷贝
        List<DishDto> dtoList = new ArrayList<>();
        for (SetmealDish dish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //对口味数据也进行封装,同时对image也封装进去
            Long dishId = dish.getDishId();
            Dish dish1 = dishService.getById(dishId);
            dishDto.setImage(dish1.getImage());
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper1);
            dishDto.setFlavors(list1);
            dtoList.add(dishDto);
        }
        return R.success(dtoList);
    }
}
